package com.mdxx.xczd.ui.activity;

import android.view.View;
import android.widget.TextView;
import com.google.gson.Gson;
import com.mdxx.xczd.R;
import com.mdxx.xczd.app.App;
import com.mdxx.xczd.bean.Wifies;
import com.mdxx.xczd.ui.base.BaseActivity;
import com.mdxx.xczd.utils.Net;
import com.mdxx.xczd.utils.ToastUtil;
import com.mdxx.xczd.utils.VersionUpdateUtil;
import com.mdxx.xczd.utils.WifiAdmin;
import com.orhanobut.logger.Logger;
import java.util.List;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by MD-02 on 2017/2/8.
 */

public class SettingsActivity extends BaseActivity{
    @BindView(R.id.tv_settings_file)
    TextView mTvFile;

    @BindView(R.id.tv_settings_settings)
    TextView mTvSettings;

    @BindView(R.id.tv_settings_wifi)
    TextView mTvWifi;

    @BindView(R.id.tv_settings_phone)
    TextView mTvphone;

    @BindView(R.id.tv_settings_about)
    TextView mTvAbout;

    @BindView(R.id.tv_settings_version)
    TextView mVersion;

    @BindView(R.id.tv_settings_logout)
    TextView mTvLogout;

    @Override
    protected int getLayout() {
        return R.layout.activity_settings;
    }

    @Override
    protected void init() {

    }

    @Override
    public void initPresenter() {}

    @OnClick({R.id.tv_settings_file, R.id.tv_settings_settings, R.id.tv_settings_wifi,
            R.id.tv_settings_phone, R.id.tv_settings_about, R.id.tv_settings_version,
            R.id.tv_settings_logout})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_settings_file:
                startActivity("net.micode.fileexplorer", "net.micode.fileexplorer.FileExplorerTabActivity");
                break;

            case R.id.tv_settings_settings:
                startActivity("com.android.settings", "com.android.settings.Settings");
                break;

            case R.id.tv_settings_wifi:
                Logger.d(new Gson().toJson(App.barCodeBean).toString());
                connectWifi(App.barCodeBean.wifies);
                break;

            case R.id.tv_settings_phone:
                startActivity(PhoneActivity.class);
                break;

            case R.id.tv_settings_about:
               startActivity(AboutActivity.class);
                break;

            case R.id.tv_settings_version:
                new VersionUpdateUtil(this).checkVersion(1);
                break;

            case R.id.tv_settings_logout:
                lognout();
                break;
        }
    }

    /**
     * 连接wifi
     */
    private void connectWifi(List<Wifies> wifis){
        if(Net.isWifiConnected(this)){
            ToastUtil.showShort("您已连上wifi");
            return;
        }
        WifiAdmin wifiAdmin = new WifiAdmin(this);
        wifiAdmin.openWifi();
        for (Wifies wifi : wifis) {
            wifiAdmin.addNetwork(wifiAdmin.CreateWifiInfo(wifi.name, wifi.password, 3));
        }
    }

    /**
     * 登出
     */
    private void lognout(){
        App.cancelAccount();
        ToastUtil.showShort("登出成功");
        removeAll();
        finish();
        startActivity(FirstActivity.class);
    }
}
